<template>
  <div class="bg">
    <div class="login">
      <h1>叩丁狼商品管理系统</h1>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        size="large"
        class="demo-ruleForm"
      >
        <el-form-item prop="username" class="login_input">
          <el-input
            v-model="ruleForm.username"
            type="text"
            autocomplete="off"
            :prefix-icon="User"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item prop="password" class="login_input">
          <el-input
            v-model.number="ruleForm.password"
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            :show-password="true"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)" style="width: 100%">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, ref } from "vue";
import { loginToken, getUserInfo } from "../../request/httpAPI";
import { ElMessage } from "element-plus";
import type { FormInstance } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import Cookies from "js-cookie"; //用于处理 cookie 的简单、轻量级 JavaScript API
// 获取el-from组件对象
import { useRouter } from "vue-router"; //路由
import { useStore } from "vuex";

const router = useRouter(); //路由对象
const store = useStore(); // vuex对象
const ruleFormRef = ref<FormInstance>();
// 数据
const ruleForm = reactive({
  username: "admin",
  password: "123456",
});
// 账号验证
const checkName = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入账号"));
  } else {
    callback();
  }
};
// 密码验证
const validatePass = (
  rule: unknown,
  value: string | undefined,
  callback: (msg?: string) => void
) => {
  if (value === "") {
    callback("请输入密码");
  } else {
    callback();
  }
};

const rules = reactive({
  password: [{ validator: validatePass, trigger: "blur" }],
  username: [{ validator: checkName, trigger: "blur" }],
});
// 提交事件(callback的写法)
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // 登录后获取token
      loginToken({
        password: ruleForm.password,
        username: ruleForm.username,
      }).then((res) => {
        if (res.code === 200) {
          // console.log(res);
          // js-cookie expires - 过期时间(day)
          Cookies.set("token", res.data.tokenHead + res.data.token, {
            expires: 7,
          });
          // 获取用户信息
          store.dispatch('updateMenus').then(res => router.push('/token')) // "token"只是一个标志,目的是要跳转到"/index"
          // ElMessage({
          //   message: "登录成功,即将跳转",
          //   type: "success",
          // });
        }
      });
    } else {
      console.log("校验失败");
      return false;
    }
  });
};
/**promise对象的写法
 * const submitForm = ()=>{
 * ruleFormRef.value.validator().then(() = > {console.log('校验通过')}).catch(()=>{'校验不通过'})}
 */
</script>
<style lang="less" scoped>
.bg {
  position: relative;
  width: 100%;
  height: 100vh;
  background-color: #2d3a4b;
  .login {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 400px;
    height: 25vh;
    font-size: 12px;
    text-align: center;
    color: #fff;
    h1 {
      margin-bottom: 10px;
    }
    .login_input {
      margin-bottom: 20px;
    }
  }
}
:deep(.el-input__inner) {
  height: 50px;
  background-color: transparent;
  border: 0;
  color: #fff;
}
</style>
